// 模块间通信接口定义
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;

// 用户管理接口
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserInterface {
    pub id: i32,
    pub username: String,
    pub email: String,
    pub full_name: Option<String>,
    pub phone: Option<String>,
    pub status: String,
    pub last_login: Option<OffsetDateTime>,
    pub created_at: OffsetDateTime,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct CreateUserRequest {
    pub username: String,
    pub email: String,
    pub password: String,
    pub full_name: Option<String>,
    pub phone: Option<String>,
    pub role_ids: Vec<i32>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct UpdateUserRequest {
    pub email: Option<String>,
    pub full_name: Option<String>,
    pub phone: Option<String>,
    pub status: Option<String>,
    pub role_ids: Option<Vec<i32>>,
}

// 网关服务接口
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RouteRuleInterface {
    pub id: i32,
    pub rule_name: String,
    pub match_pattern: String,
    pub target_url: String,
    pub method: String,
    pub priority: i32,
    pub is_active: bool,
    pub timeout_ms: i32,
    pub retry_count: i32,
    pub rate_limit: i32,
    pub headers_config: serde_json::Value,
    pub rewrite_config: serde_json::Value,
    pub created_at: OffsetDateTime,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GatewayConfigInterface {
    pub id: i32,
    pub config_key: String,
    pub config_value: serde_json::Value,
    pub description: Option<String>,
    pub config_type: String,
    pub is_active: bool,
    pub created_at: OffsetDateTime,
}

// 菜单管理接口
#[derive(Debug, Serialize, Deserialize)]
pub struct MenuInterface {
    pub id: i32,
    pub parent_id: Option<i32>,
    pub menu_name: String,
    pub menu_path: Option<String>,
    pub menu_icon: Option<String>,
    pub menu_type: String,
    pub sort_order: i32,
    pub is_visible: bool,
    pub permissions: serde_json::Value,
    pub created_at: OffsetDateTime,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct MenuTreeInterface {
    pub id: i32,
    pub menu_name: String,
    pub menu_path: Option<String>,
    pub menu_icon: Option<String>,
    pub menu_type: String,
    pub sort_order: i32,
    pub is_visible: bool,
    pub permissions: serde_json::Value,
    pub children: Vec<MenuTreeInterface>,
}

// 认证接口
#[derive(Debug, Serialize, Deserialize)]
pub struct LoginRequest {
    pub username: String,
    pub password: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct LoginResponse {
    pub token: String,
    pub refresh_token: String,
    pub expires_in: u64,
    pub user: UserInterface,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TokenRefreshRequest {
    pub refresh_token: String,
}

// 通用响应接口
#[derive(Debug, Serialize, Deserialize)]
pub struct ApiResponse<T> {
    pub success: bool,
    pub data: Option<T>,
    pub message: String,
    pub code: i32,
}

impl<T> ApiResponse<T> {
    pub fn success(data: T) -> Self {
        Self {
            success: true,
            data: Some(data),
            message: "操作成功".to_string(),
            code: 200,
        }
    }

    pub fn error(message: String, code: i32) -> Self {
        Self {
            success: false,
            data: None,
            message,
            code,
        }
    }

    pub fn unauthorized() -> Self {
        Self {
            success: false,
            data: None,
            message: "未授权访问".to_string(),
            code: 401,
        }
    }
}

// 分页接口
#[derive(Debug, Serialize, Deserialize)]
pub struct PaginationRequest {
    pub page: i32,
    pub page_size: i32,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PaginationResponse<T> {
    pub data: Vec<T>,
    pub total: i64,
    pub page: i32,
    pub page_size: i32,
    pub total_pages: i32,
}

// 模块间消息传递接口
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ModuleMessage {
    UserCreated(UserInterface),
    UserUpdated(UserInterface),
    UserDeleted(i32),
    RouteRuleCreated(RouteRuleInterface),
    RouteRuleUpdated(RouteRuleInterface),
    RouteRuleDeleted(i32),
    ConfigUpdated(GatewayConfigInterface),
}

// 错误类型定义
#[derive(Debug, Serialize, Deserialize)]
pub struct ErrorResponse {
    pub code: i32,
    pub message: String,
    pub details: Option<serde_json::Value>,
}

// 服务发现接口
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceInfo {
    pub service_name: String,
    pub service_url: String,
    pub health_check_url: String,
    pub version: String,
    pub metadata: serde_json::Value,
}

// 服务健康检查结果
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceHealthResult {
    pub service_name: String,
    pub status: String,
    pub timestamp: i64,
}

// 监控指标接口
#[derive(Debug, Serialize, Deserialize)]
pub struct MetricsData {
    pub timestamp: OffsetDateTime,
    pub service_name: String,
    pub metric_name: String,
    pub value: f64,
    pub tags: serde_json::Value,
}